package com.example.hao.circleicon;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_show;
    private int widthPixels;
    private Bitmap bitmap;
    private Canvas canvas;
    private Paint paint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        circleIcon();
        iv_show.setImageBitmap(bitmap);
    }


    private void initView() {
        iv_show = findViewById(R.id.iv_show);

        widthPixels = getResources().getDisplayMetrics().widthPixels;
        bitmap = Bitmap.createBitmap(widthPixels, widthPixels, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();

    }
    //圆形头像
    private void circleIcon() {

        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawCircle(widthPixels / 2, widthPixels / 2, widthPixels / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.man);
        RectF dis = new RectF(0, 0, widthPixels, widthPixels);
        canvas.drawBitmap(bitmap, null, dis, paint);
    }
}
